import {
  listByUserId,
  del
} from '../../api/ai'


// pages/collect/index.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    slideButtons: [{
      text: '删除',
      type: 'warn'
    }],
    list: []
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    listByUserId().then(res => {
      this.setData({
        list: res
      })
    })
  },

  cancelCollect(e) {
    const id = e.currentTarget.dataset.id
    del(id).then(() => {
      const newList = []
      this.data.list.forEach(element => {
        if (element.id !== id) {
          newList.push(element)
        }
      })
      this.setData({
        list: newList
      })
    })
  },
  toDetail(e) {
    const id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: `/pages/robot/index?id=${id}`,
    })
  },
  add() {
    if (this.data.list.length === 5) {
      wx.showModal({
        content: '会话已达上限',
        showCancel: false,
        confirmText: '我知道了'
      })
      return false
    }

    wx.navigateTo({
      url: `/pages/robot/index`,
    })
  }

})